Nested VMX: Emulation of VMRESUME/VMLAUNCH
authorEddie Dong <eddie.dong@intel.com>
Thu, 9 Jun 2011 08:24:09 +0000 (16:24 +0800)
committerEddie Dong <eddie.dong@intel.com>
Thu, 9 Jun 2011 08:24:09 +0000 (16:24 +0800)
Signed-off-by: Qing He <qing.he@intel.com>
Signed-off-by: Eddie Dong <eddie.dong@intel.com>
Acked-by: Tim Deegan <Tim.Deegan@citrix.com>
Committed-by: Tim Deegan <Tim.Deegan@citrix.com>
xen/arch/x86/hvm/vmx/vmx.c
xen/arch/x86/hvm/vmx/vvmx.c
xen/include/asm-x86/hvm/vmx/vvmx.h

index 5b6a088b78e9f128a3a0333b8cf38812a53a711f..169a5ce0d11845542030cee68a94cf5faee4e35b 100644 (file)
@@ -2170,6 +2170,11 @@ asmlinkage void vmx_vmexit_handler(struct cpu_user_regs *regs)
     /* Now enable interrupts so it's safe to take locks. */
     local_irq_enable();
 
+    /* XXX: This looks ugly, but we need a mechanism to ensure
+     * any pending vmresume has really happened
+     */
+    vcpu_nestedhvm(v).nv_vmswitch_in_progress = 0;
+
     if ( unlikely(exit_reason & VMX_EXIT_REASONS_FAILED_VMENTRY) )
         return vmx_failed_vmentry(exit_reason, regs);
 
@@ -2464,10 +2469,18 @@ asmlinkage void vmx_vmexit_handler(struct cpu_user_regs *regs)
             update_guest_eip();
         break;
 
-    case EXIT_REASON_MWAIT_INSTRUCTION:
-    case EXIT_REASON_MONITOR_INSTRUCTION:
     case EXIT_REASON_VMLAUNCH:
+        if ( nvmx_handle_vmlaunch(regs) == X86EMUL_OKAY )
+            update_guest_eip();
+        break;
+
     case EXIT_REASON_VMRESUME:
+        if ( nvmx_handle_vmresume(regs) == X86EMUL_OKAY )
+            update_guest_eip();
+        break;
+
+    case EXIT_REASON_MWAIT_INSTRUCTION:
+    case EXIT_REASON_MONITOR_INSTRUCTION:
     case EXIT_REASON_GETSEC:
     case EXIT_REASON_INVEPT:
     case EXIT_REASON_INVVPID:
index d321c9e8f184697ce8cc5971a556ad957d78f8e9..1847339631255134365463b6f83f8843f447d4bf 100644 (file)
@@ -261,6 +261,13 @@ static void reg_write(struct cpu_user_regs *regs,
     }
 }
 
+static inline u32 __n2_exec_control(struct vcpu *v)
+{
+    struct nestedvcpu *nvcpu = &vcpu_nestedhvm(v);
+
+    return __get_vvmcs(nvcpu->nv_vvmcx, CPU_BASED_VM_EXEC_CONTROL);
+}
+
 static int vmx_inst_check_privilege(struct cpu_user_regs *regs, int vmxop_check)
 {
     struct vcpu *v = current;
@@ -486,6 +493,62 @@ int nvmx_handle_vmxoff(struct cpu_user_regs *regs)
     return X86EMUL_OKAY;
 }
 
+int nvmx_vmresume(struct vcpu *v, struct cpu_user_regs *regs)
+{
+    struct nestedvmx *nvmx = &vcpu_2_nvmx(v);
+    struct nestedvcpu *nvcpu = &vcpu_nestedhvm(v);
+    int rc;
+
+    rc = vmx_inst_check_privilege(regs, 0);
+    if ( rc != X86EMUL_OKAY )
+        return rc;
+
+    /* check VMCS is valid and IO BITMAP is set */
+    if ( (nvcpu->nv_vvmcxaddr != VMCX_EADDR) &&
+            ((nvmx->iobitmap[0] && nvmx->iobitmap[1]) ||
+            !(__n2_exec_control(v) & CPU_BASED_ACTIVATE_IO_BITMAP) ) )
+        nvcpu->nv_vmentry_pending = 1;
+    else
+        vmreturn(regs, VMFAIL_INVALID);
+
+    return X86EMUL_OKAY;
+}
+
+int nvmx_handle_vmresume(struct cpu_user_regs *regs)
+{
+    int launched;
+    struct vcpu *v = current;
+
+    launched = __get_vvmcs(vcpu_nestedhvm(v).nv_vvmcx,
+                           NVMX_LAUNCH_STATE);
+    if ( !launched ) {
+       vmreturn (regs, VMFAIL_VALID);
+       return X86EMUL_EXCEPTION;
+    }
+    return nvmx_vmresume(v,regs);
+}
+
+int nvmx_handle_vmlaunch(struct cpu_user_regs *regs)
+{
+    int launched;
+    int rc;
+    struct vcpu *v = current;
+
+    launched = __get_vvmcs(vcpu_nestedhvm(v).nv_vvmcx,
+                           NVMX_LAUNCH_STATE);
+    if ( launched ) {
+       vmreturn (regs, VMFAIL_VALID);
+       rc = X86EMUL_EXCEPTION;
+    }
+    else {
+        rc = nvmx_vmresume(v,regs);
+        if ( rc == X86EMUL_OKAY )
+            __set_vvmcs(vcpu_nestedhvm(v).nv_vvmcx,
+                        NVMX_LAUNCH_STATE, 1);
+    }
+    return rc;
+}
+
 int nvmx_handle_vmptrld(struct cpu_user_regs *regs)
 {
     struct vcpu *v = current;
index efc62e1f1c31501cd7619ed4ff067b82b235bd8a..35a184b3206afeb0e34762c18aa7900b29c7cef5 100644 (file)
@@ -158,6 +158,8 @@ int nvmx_handle_vmptrst(struct cpu_user_regs *regs);
 int nvmx_handle_vmclear(struct cpu_user_regs *regs);
 int nvmx_handle_vmread(struct cpu_user_regs *regs);
 int nvmx_handle_vmwrite(struct cpu_user_regs *regs);
+int nvmx_handle_vmresume(struct cpu_user_regs *regs);
+int nvmx_handle_vmlaunch(struct cpu_user_regs *regs);
 
 #endif /* __ASM_X86_HVM_VVMX_H__ */